//
// Created by mio on 25-5-9.
//

#include <QVBoxLayout>

#include "ListinfoWgt.h"
#include "../tool/ListWidget.h"
#include "../tool/CustomWgt.h"
#include "../tool/StyleLoader.h"


ListinfoWgt::ListinfoWgt(QWidget *parent)
    : QWidget{parent}
{
    setWindowFlag(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_StyledBackground);
    setFixedSize(200,540);
    // setStyleSheet("background-color: #563149");

    userBtn_ = new QPushButton(this);
    listWgt_ = new ListWidget(this);

    userBtn_->setObjectName("user_Btn");
    listWgt_->setObjectName("listWidget");

    connect(listWgt_, &ListWidget::itemClicked, this, &ListinfoWgt::HandleItemSelect);
    connect(userBtn_, &QPushButton::clicked, this, [this]() {
        emit sig_Select(0);
        for(auto item : customWgts_) {
            item->setHightLight(false);
        }
        listWgt_->clearSelection();
    });

    // 添加Item
    CustomWgt* rmtWgt = new CustomWgt(this);
    CustomWgt* dvcWgt = new CustomWgt(this);
    CustomWgt* setWgt = new CustomWgt(this);

    rmtWgt->setImageAndText("远程控制", ":/UI/brown/list/remote.png", ":/UI/brown/list/remote_press.png", true);
    dvcWgt->setImageAndText("设备列表", ":/UI/brown/list/device.png", ":/UI/brown/list/device_press.png");
    setWgt->setImageAndText("高级设置", ":/UI/brown/list/setting.png", ":/UI/brown/list/setting_press.png");

    // 将这些CustomWgt存起来
    customWgts_.push_back(rmtWgt);
    customWgts_.push_back(dvcWgt);
    customWgts_.push_back(setWgt);

    listWgt_->AddWidget(rmtWgt);
    listWgt_->AddWidget(dvcWgt);
    listWgt_->AddWidget(setWgt);

    userBtn_->setFixedSize(100, 100);
    listWgt_->setFixedSize(200, 340);

    // 布局
    QVBoxLayout* layout = new QVBoxLayout(this);
    layout->addSpacing(50);
    layout->addWidget(userBtn_, 0, Qt::AlignHCenter);
    layout->addSpacing(50);
    layout->addWidget(listWgt_, 1, Qt::AlignHCenter);
    layout->setContentsMargins(0, 0, 0, 0);
    setLayout(layout);

    StyleLoader::getInstance()->loadStyle(":/UI/brown/main.css", this);
}

void ListinfoWgt::HandleItemSelect(int index)
{
    if(index < 0 || index >= customWgts_.size()) { return; }
    emit sig_Select(index + 1);
    for(int i = 0; i < customWgts_.size(); i++) {
        if(i == index)
        {
            customWgts_[i]->setHightLight(true); // 设置高亮
        }
        else {  customWgts_[i]->setHightLight(false); }
    }
}
